// pages/returnaddressedit/returnaddressedit.js
import { areaList } from '~/miniprogram_npm/@vant/area-data/index';
import baseteam from '~/utils/teamleader.js'
import basesup from '~/utils/supplier.js'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        checked: false,
        areaList,
        changeareadata: [],
        shew3: false,
        addressid: '',
        sjkid:'',
        diqu:'',
    },
    naveback() {
        setTimeout(() => {
            wx.navigateBack()
        }, 300);
    },
    locations() {
        this.setData({ shew3: true });
    },
    onClose3() {
        this.setData({ shew3: false });
    },
    confirm3(item) {
        let diqu = ''
        for (const i in item.detail.values) {
            diqu = diqu + item.detail.values[i].name
        }
        console.log(diqu)
        this.setData({
            changeareadata: item.detail.values,
            diqu: diqu
        });
        this.onClose3()
    },
    onClickIcon() {
        this.locations()
    },
    switchonChange(item) {
        this.setData({ checked: item.detail });
    },
    async getressid() {
        let reqdata = {
            addressid: this.data.addressid
        }
        let reqmsg = await baseteam.reqaddress(reqdata)
        if (reqmsg.code == 200) {
            let newdefault = false
            if ( reqmsg.data.Result.isDefault == 1) {
                newdefault = true
            }
            console.log(reqmsg)
            this.setData({
                "sjkid":reqmsg.data.Result.id,
                "recipients": reqmsg.data.Result.recipients,
                "phone": reqmsg.data.Result.phone,
                "diqu": reqmsg.data.Result.province,
                "provinceId": reqmsg.data.Result.provinceId,
                "changeareadata": JSON.parse(reqmsg.data.Result.provinceCode),
                "detailedAddress": reqmsg.data.Result.detailedAddress,
                'checked': newdefault
            })
        }
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        if ((typeof(options.ressid)) === "string") {
            this.setData({
                addressid: options.ressid
            })
            this.getressid()
        }
    },
    async addokback() {
        let newdefault = 2
        if (this.data.checked == true) {
            newdefault = 1
        }
        if (this.data.recipients == '') {
            wx.showToast({
                title: '收件人还未输入',
                icon: 'none',
                duration: 2000
            })
            return false
        }
        if (this.data.phone == '') {
            wx.showToast({
                title: '手机号还未输入',
                icon: 'none',
                duration: 2000
            })
            return false
        }
        if (this.data.diqu == '') {
            wx.showToast({
                title: '地区还未选择',
                icon: 'none',
                duration: 2000
            })
            return false
        }
        let ressssq = JSON.stringify(this.data.changeareadata)
        console.log(ressssq)
        if (this.data.detailedAddress == '') {
            wx.showToast({
                title: '详细地址还未输入',
                icon: 'none',
                duration: 2000
            })
            return false
        }
        let reqdata = {
            "recipients": this.data.recipients,
            "phone": this.data.phone,
            "province": this.data.diqu,
            "provinceId": this.data.changeareadata[this.data.changeareadata.length - 1].code,
            "provinceCode":ressssq,
            "detailedAddress": this.data.detailedAddress,
            'isDefault': newdefault,
            "returnState": 1
        }
        console.log(reqdata)
        let reqstart = await basesup.returnaddress(reqdata)
        if (reqstart.code == 200) {
            wx.showToast({
                title: '添加/保存成功',
                icon: 'success',
                duration: 2000
            })
            setTimeout(() => {
                this.naveback()
            }, 2000);
        }
    },

    async updataokback(){
        let newdefault = 2
        if (this.data.checked == true) {
            newdefault = 1
        }
        if (this.data.recipients == '') {
            wx.showToast({
                title: '收件人还未输入',
                icon: 'none',
                duration: 2000
            })
            return false
        }
        if (this.data.phone == '') {
            wx.showToast({
                title: '手机号还未输入',
                icon: 'none',
                duration: 2000
            })
            return false
        }
        if (this.data.diqu == '') {
            wx.showToast({
                title: '地区还未选择',
                icon: 'none',
                duration: 2000
            })
            return false
        }
        let ressssq = JSON.stringify(this.data.changeareadata)
        if (this.data.detailedAddress == '') {
            wx.showToast({
                title: '详细地址还未输入',
                icon: 'none',
                duration: 2000
            })
            return false
        }
        let reqdata = {
            "id":this.data.sjkid,
            "recipients": this.data.recipients,
            "phone": this.data.phone,
            "province": this.data.diqu,
            "provinceId": this.data.changeareadata[this.data.changeareadata.length - 1].code,
            "provinceCode":ressssq,
            "detailedAddress": this.data.detailedAddress,
            'isDefault': newdefault,
            "returnState": 1
        }
        console.log(reqdata)
        let reqstart = await basesup.updatereturnaddress(reqdata)
        if (reqstart.code == 200) {
            wx.showToast({
                title: '添加/保存成功',
                icon: 'success',
                duration: 2000
            })
            setTimeout(() => {
                this.naveback()
            }, 2000);
        }
    },
    async deladdress(){
        let reqdata = {
            id:this.data.sjkid
        }
        let resdata = await baseteam.deladdress(reqdata)
        if (resdata.code == 200) {
            wx.showToast({
                title: '删除成功',
                icon: 'success',
                duration: 2000
            })
            this.naveback()
        }else{
            wx.showToast({
                title: '删除失败',
                icon: 'error',
                duration: 2000
            })
        }
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})